package com.education.wisdom.controller.mq;

import com.alibaba.fastjson.JSON;
import com.education.common.config.WebSocketServer;
import com.education.wisdom.pojo.CurrencyRecord;
import com.education.wisdom.service.ICurrencyRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


//@RestController
public class Consumer {

    @Autowired
    private ICurrencyRecordService currencyRecordService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Logger logger = LoggerFactory.getLogger(getClass());

    @RabbitListener(queues = "wisdom_queue_message")//监听的队列名称 wisdom_queue_message
    @RabbitHandler
    public void test(Map map) {
        String message = JSON.toJSONString(map);
        logger.info("rabbitmq信息 : {}", message);

        try {
            //调用方法推送消息
//            WebSocketServer.sendInfo(message,map.get("userId").toString());
            WebSocketServer.sendInfoAll(message);
        } catch (IOException e) {
            logger.error("消息发送失败");
        }
    }

//    //监听消息修改订单状态为支付失败
//    @RabbitListener(queues = "wisdom_queue_dead")//监听的队列名称 wisdom_queue_dead
//    @RabbitHandler
//    public void test(String message) {
//        logger.info("死信队列 : {} --- 时间 : {}",message,simpleDateFormat.format(new Date()));
//        CurrencyRecord record = new CurrencyRecord();
//        record.setCurrencyId(message);//订单号
//        record.setStatus("0");//支付状态 1成功  0 支付失败 2 支付中
//        record.setEditTime(new Date());//修改时间
//        currencyRecordService.updateCurrencyRecord(record);
//    }

}
